import { Module } from 'vuex'
import { ISystemState } from './type'
import { IRootState } from '@/store/types'

import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '@/service/main/system/ststem'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [], // 商品数组
      goodsCount: 0,
      menuList: [], // 菜单列表
      menuCount: 0,
      storyList: [], // 故事列表
      storyCount: 0,
      categoryList: [], // 商品类别
      categoryCount: 0,
      departmentList: [], // 商品类别
      departmentCount: 0
    }
  },
  mutations: {
    changeUsersList(state, userList: []) {
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeRoleList(state, list: []) {
      state.roleList = list
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, list: []) {
      state.goodsList = list
    },
    changeGoodsCount(state, count: number) {
      state.goodsCount = count
    },
    changeMenuList(state, list: []) {
      state.menuList = list
    },
    changeMenuCount(state, count: number) {
      state.menuCount = count
    },
    changeStoryList(state, list: []) {
      state.storyList = list
    },
    changeStoryCount(state, count: number) {
      state.storyCount = count
    },
    changeCategoryList(state, list: []) {
      state.categoryList = list
    },
    changeCategoryCount(state, count: number) {
      state.categoryCount = count
    },
    changeDepartmentList(state, list: []) {
      state.departmentList = list
    },
    changeDepartmentCount(state, count: number) {
      state.departmentCount = count
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  actions: {
    async getPageListAction({ commit }, payLoad: any) {
      // 1 获取pageUrl
      const pageName = payLoad.pageName
      const pageUrl = `${pageName}/List`

      // 2 对页面发送请求
      const pageResult = await getPageListData(pageUrl, payLoad.queryInfo)

      const { list, totalCount } = pageResult.data

      const changePageName = pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${changePageName}List`, list)
      commit(`change${changePageName}Count`, totalCount)
    },
    async deletePageDataAction(context, payLoad: any) {
      // 获取pageName 和id
      // 1 pageName -> /users
      // 2 id -> /users/id
      const { pageName, id } = payLoad
      const pageUrl = `/${pageName}/${id}`
      // 2 调用删除网络请求
      await deletePageData(pageUrl)
      // 3.重新请求最新的数据
      context.dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async createPageDataAction({ dispatch }, payload: any) {
      // 1 创建数据的请求
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      // 2.重新请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async editPageDataAction({ dispatch }, payload: any) {
      // 1 编辑数据的请求
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)
      // 2.重新请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}

export default systemModule
